package laba7.taskB;

import interfaces.task7.executor.CopyTask;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * Class for copying files.
 *
 * @author yakimovich
 */
public class CopyTaskImpl implements CopyTask {

	/**
	 * The source file.
	 */
	private File source;
	/**
	 * The destination file.
	 */
	private File dest;
	/**
	 * The number of attempts.
	 */
	private int tryCount;

	/**
	 * Create CopyTaskImpl by default.
	 */
	public CopyTaskImpl() {
	}

	/**
	 * Create CopyTaskImpl with with the specified parameters.
	 *
	 * @param source
	 *            the file-source
	 * @param dest
	 *            the file-destination
	 */
	public CopyTaskImpl(File source, File dest) {
		this.source = source;
		this.dest = dest;
	}

	/**
	 * The method sets the source file for this task.
	 *
	 * @param string
	 *            name of the source file
	 */
	@Override
	public void setSource(String string) {
		source = new File(string);
		try {
			if (!source.exists() || !source.isFile()) {
				throw new FileNotFoundException();
			}
		} catch (FileNotFoundException e) {
			throw new IllegalArgumentException(
					"File does not exist", e);
		}
	}

	/**
	 * The method sets the destination file for this task.
	 *
	 * @param string
	 *            name of the destination file
	 */
	@Override
	public void setDest(String string) {
		dest = new File(string);
	}

	/**
	 * The method returns return the number of try count.
	 *
	 * @return the number of try count
	 */
	@Override
	public int getTryCount() {
		return tryCount;
	}

	/**
	 * The method increments tryCount.
	 */
	@Override
	public void incTryCount() {
		tryCount++;
	}

	/**
	 * Method for copying file.
	 *
	 * @return true if copying is successful and false if copying is failed
	 * @throws Exception
	 *             when copying is failed and tryCount is 5
	 */
	@Override
	public boolean execute() throws Exception {
            boolean isSuccess = false;
		FileChannel inChannel;
        FileChannel outChannel;
        try {
            inChannel = new FileInputStream(source).getChannel();
            outChannel = new FileOutputStream(dest).getChannel();
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException("Input File Not Found", e);
        }
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
            isSuccess = true;
        } catch (IOException e) {
            System.out.println(e);
        } finally {
            try {
                if (inChannel != null) {
                    inChannel.close();
                }
                if (outChannel != null) {
                    outChannel.close();
                }
            } catch (IOException e) {
                System.out.println(e);
            }
        }
        System.out.println("Copy: " + isSuccess);
            return isSuccess;
    }
}
